<?php
namespace app\controller;
use app\BaseController;

use think\facade\Db;
use think\Request;
use think\captcha\facade\Captcha;
use think\cache\driver\Redis;
use think\facade\Cache;

/**
* 
*/
class Reg extends BaseController
{
	protected $userInfo;
	protected $request;
	public function __construct(Request $request){
        $this->request = $request;
	}

	public function index(){
		$data = Db::table("config")->where("label","reg")->select()->toArray();

		return json_encode(array("code"=>200,"msg"=>'成功','data'=>$data));
	}
	/**
	 * 注册
	 * @return json_encode([type] [description]
	 */
	public function reg(){
		$redis_1 = new Redis();
        $redis_1->handler()->select(10);
		$requests = new Request_type();

		$regConfig = $redis_1->get("yt_reg");
		$regConfig = json_decode($regConfig,true);
		if($regConfig['switch'] != 1){
			return json_encode(array("code"=>'109',"msg"=>'暫未開放註冊帳戶'));
		}
		$data = $this->request->param();
		if(!isset($data['mobile']) || $data['mobile'] == ""){
			return json_encode(array("code"=>'101',"msg"=>'手機號不能為空'));
		}
		if(!preg_match("/^1[3456789]\d{9}$/",$data['mobile']))
        {
            return json_encode(array("code"=>'124',"msg"=>'手機號格式錯誤'));
        }
		if(Db::table("t_user")->where("user_name",$data['mobile'])->find()){
		    return json_encode(array("code"=>'123',"msg"=>'該帳戶已存在'));
		}
		if($regConfig['code'] != 1){
			$redis = new Redis();
	        $redis->handler()->select(10);
			$yzm = $redis->get($data['mobile']);
			if(!$yzm){
				return json_encode(array("code"=>'102',"msg"=>'請先獲取驗證碼'));
			}
			if($data['mobile_yzm'] == "" || $data['mobile_yzm'] == "null" || !isset($data['mobile_yzm'])){
				return json_encode(array("code"=>'103',"msg"=>'簡訊驗證碼不能為空'));
			}else{
				$redis->delete($data['mobile']);
			}
			if($yzm != $data['mobile_yzm']){
				return json_encode(array("code"=>'104',"msg"=>'簡訊驗證碼不正確'));
			}
		}		
		if(!isset($data['password']) || $data['password'] == ""){
			return json_encode(array("code"=>'105',"msg"=>'登入密碼不能為空'));
		}
		if(!isset($data['pay_password']) || $data['pay_password'] == ""){
			return json_encode(array("code"=>'106',"msg"=>'支付密碼不能為空'));
		}
		// if($data['pay_password'] == $data['password']){
		// 	return json_encode(array("code"=>'107',"msg"=>'支付密碼與登入密碼不能一致'));
		// }
	    $userInfo = Db::table("t_user")->where("invite_code",$data['invite_code'])->find();
		if(!$userInfo){
			return json_encode(array("code"=>'108',"msg"=>'邀請碼不正確，請先確認！！'));
		}
		$referrer = $userInfo['id'];
		$referrer_list = $userInfo['referrer_list'].$userInfo['id'].",";

    		
		$salt = $requests->salt(rand(6,9));
		$pay_salt = $requests->salt(rand(6,9));

		$dataArray=array(
			"user_name"=>$data['mobile'],
			"mobile"=>$data['mobile'],
			"invite_code"=>$this->invite(),
			"referrer"=>$referrer,
			"referrer_list"=>$referrer_list,
			"salt"=>$salt,
			"password"=>md5(md5($data['password'].$salt)),
			"pay_salt"=>$pay_salt,
			"pay_password"=>md5(md5($data['pay_password'].$pay_salt)),
			"register_time"=>date("Y-m-d H:i:s"),
			"status"=>0,
			"area_code"=>"+86",
			"register_ip"=>$requests->get_client_ip()
		);
		
		if(Db::table("t_user")->save($dataArray)){
		    $user_id = Db::name('t_user')->getLastInsID();
		    $requests->curl_post("http://yituan.bymall.co:8089/wallet/bind-address",array("userId"=>$user_id,'coinname'=>'USDT'));
			return json_encode(array("code"=>200,"msg"=>'成功'));
		}else{
			return json_encode(array("code"=>100,"msg"=>'失敗'));
		}
	}

	/**
	 * 找回密码
	 * @return json_encode([type] [description]
	 */
	public function retrieve_psw(){

	}
	
	/**
	* 获取邀请码 -- 唯一
	*/
	public function invite(){
		$requests = new Request_type();
		$invite = $requests->invite();
		if(Db::table("t_user")->where("invite_code",$invite)->find()){
			$this->invite();
		}else{
			return $invite;
		}
	}
}
?>